﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI;
using MyLib.Data;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	public class MyCodeDataSourceView : DataSourceView {
		private MyCodeDataSource m_owner;

		private int m_categoryId;

		private bool m_prependAllCode;

		private bool m_prependEmptyCode;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="owner"></param>
		/// <param name="viewName"></param>
		/// <param name="categoryId"></param>
		/// <param name="prependAllCode"></param>
		/// <param name="prependEmptyCode"></param>
		public MyCodeDataSourceView(MyCodeDataSource owner, string viewName, int categoryId, bool prependAllCode, bool prependEmptyCode)
			: base(owner, viewName) {
			System.Diagnostics.Debug.Assert(owner != null);
			System.Diagnostics.Debug.Assert(!(prependAllCode && prependEmptyCode));

			m_owner = owner;
			m_categoryId = categoryId;
			m_prependAllCode = prependAllCode;
			m_prependEmptyCode = prependEmptyCode;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="arguments"></param>
		/// <returns></returns>
		protected override System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments) {
			arguments.RaiseUnsupportedCapabilitiesError(this);

			var t = new CodeTable();

			m_owner.GetDataTable().CopyToDataTable(t, LoadOption.OverwriteChanges);

			if (m_prependAllCode) {
				var arow = t.NewRow();
				arow[1] = string.Empty;
				arow[2] = MyLib.Web.Properties.Main.AllCodeText;

				t.Rows.InsertAt(arow, 0);
			} else if (m_prependEmptyCode) {
				var arow = t.NewRow();
				arow[1] = string.Empty;
				arow[2] = MyLib.Web.Properties.Main.EmptyCodeText;

				t.Rows.InsertAt(arow, 0);
			}

			t.AcceptChanges();

			return new DataView(t);
		}
	} // end of MyCodeDataSourceView.
}
